<template>
  <el-dialog
    v-el-drag-dialog
    title="处理确认"
    :visible.sync="visible"
    width="600px"
    @close="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" label-position="right" label-width="80px">
      <el-form-item label="客户名称" class="block">
        <el-input v-model="temp.csmName" disabled />
      </el-form-item>
      <el-form-item label="客户地址" class="block">
        <el-input v-model="temp.csmAddress" disabled />
      </el-form-item>
      <el-form-item v-if="isExist === 1&&!!(corpSettingData.sp4 >> 22 & 0x01)" class="block" label="退单理由" prop="checkedTypes">
        <el-checkbox-group v-model="checkedTypes">
          <el-checkbox v-for="item in commonKeysInfoList" :key="item.keyId" :value="item.keyId" :label="item.keyName" />
        </el-checkbox-group>
      </el-form-item>
      <el-form-item class="block" label="备注:" prop="refundReason">
        <el-input v-model="temp.refundReason" type="textarea" :rows="3" :maxlength="isExist === 0 ? 250 : 100" show-word-limit />
        <!-- <el-input v-model="temp.refundReason" type="textarea" :rows="3" maxlength="250" show-word-limit /> -->
      </el-form-item>
      <el-form-item v-if="isLinePay" label-width="0">
        <el-checkbox v-model="checked">操作提示：此订单已线上支付,将同时产生退款申请单</el-checkbox>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryRefundOrderBack, queryRefundOrderDetailData } from '@/api'
  import { mapGetters } from 'vuex'
  export default {
    name: 'back-order',
    data() {
      return {
        visible: false,
        isLinePay: false,
        orderCode: '',
        checked: true,
        isExist: 1,
        temp: {
          refundReason: ''
        },
        detail: {},
        commonKeysInfoList: [], // 退单理由复选框
        checkedTypes: []
      }
    },
    computed: {
      ...mapGetters(['orgList', 'corpSettingData'])
    },
    methods: {
      open(temp) {
        this.getOrderDetail(temp.orderCode)
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp, {
              refundReason: temp.remark
            })
          }
        })
      },
      async getOrderDetail(orderCode) {
        const res = await queryRefundOrderDetailData({ orderCode })
        this.detail = JSON.parse(res.data.orderBillInfo)
        this.isLinePay = JSON.parse(res.data.isLinePay)
        this.commonKeysInfoList = JSON.parse(res.data.commonKeysInfoList)
        this.isExist = Number(res.data.isExist)
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          let payStatus = 0
          if (this.checked) {
            payStatus = this.detail.payStatus
          }
          const params = {
            orderCode: this.temp.orderCode,
            payStatus: payStatus,
            refundReason: this.temp.refundReason,
            refundReasonHidden: this.temp.remark
          }
          if (this.isExist === 0) {
            params.refundReason = this.temp.refundReason
          } else {
            let reason = ''
            if (this.checkedTypes.length === 0 && !!(this.corpSettingData.sp4 >> 22 & 0x01)) {
              this.$message.warning('退单原因不能为空')
              return
            }
            if (this.checkedTypes.length > 0) {
              this.checkedTypes.forEach(item => {
                reason += `${item}，`
              })
              reason = reason.slice(0, -1)
            }
            const reason2 = this.temp.refundReason
            params.backReason = reason
            params.refundReason = reason2
            // const reason2 = this.temp.refundReason
            // if (reason !== '' && reason2 === '') {
            //   params.refundReason = reason.substring(0, reason.length - 1)
            // } else {
            //   params.refundReason = reason + reason2
            // }
            if (params.refundReason.length >= 255) {
              this.$message.error('退单原因字数过长')
              return
            }
          }
          this.submit(params)
        })
      },
      async submit(params) {
        await queryRefundOrderBack(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '操作成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          refundReason: ''
        }
        this.checkedTypes = []
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  & /deep/ .el-form-item {
    width: 48%;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
